package com.epoint.clubmanagement.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.epoint.clubmanagement.common.ResultMapUtil;
import com.epoint.clubmanagement.entity.User;
import com.epoint.clubmanagement.service.IUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;

/**
 * 用户相关的controller
 *
 * @author SQN
 */
@Controller
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 转向登录页面
     */
    @RequestMapping(value = "/login")
    public String login() {
        return "/login";
    }

    /**
     * 判断用户登录是否成功
     */
    @RequestMapping(value = "/toLogin")
    @ResponseBody
    public Object toLogin(String username, String password, Model model) {
        if (username == null || password == null) {
            return ResultMapUtil.getHashMapLogin("用户名密码不能为空", "2");
        }
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try {
            subject.login(token);
            subject.getSession().setAttribute("username", username);
        } catch (UnknownAccountException e) {
            return ResultMapUtil.getHashMapLogin("用户名不存在", "2");
        } catch (IncorrectCredentialsException e) {
            return ResultMapUtil.getHashMapLogin("密码错误", "2");
        }
        return ResultMapUtil.getHashMapLogin("验证成功", "1");
    }

    /**
     * 转向后台管理首页
     */
    @RequestMapping(value = "/index")
    public String index() {
        return "/index";
    }

    /**
     * 退出登录
     */
    @RequestMapping(value = "/logout")
    public String logout(Model model) {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        subject.getSession().removeAttribute("username");
        return "redirect:/login";
    }

    /**
     * 转向用户页面
     */
    @RequestMapping(value = "/user")
    public String user() {
        return "/user";
    }

    /**
     * 分页查询用户列表
     */
    @RequestMapping(value = "/user/userQueryPage")
    @ResponseBody
    public Object userQueryPage(String param, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        try {
            IPage<User> iPage = userService.selectUserPage(pageNum, pageSize, param);
            return ResultMapUtil.getHashMapMysqlPage(iPage);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 转向用户新增页面
     */
    @RequestMapping(value = "/user/userPage")
    public String userPage() {
        return "/userPage";
    }

    /**
     * 添加一个用户
     */
    @RequestMapping(value = "/user/userAdd")
    @ResponseBody
    public Object userAdd(@Valid User user) {
        try {
            int i = userService.addUser(user);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 转向用户编辑页面
     */
    @RequestMapping(value = "/user/userQueryById")
    public String userQueryById(@RequestParam(name = "id", required = true) Integer id, Model model) {
        User user = userService.queryUserById(id);
        model.addAttribute("obj", user);
        return "/userPage";
    }

    /**
     * 修改一个用户
     */
    @RequestMapping(value = "/user/userEdit")
    @ResponseBody
    public Object userEdit(@Valid User user) {
        try {
            int i = userService.editUser(user);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 删除一个用户
     */
    @RequestMapping(value = "/user/userDelById")
    @ResponseBody
    public Object userDelById(@RequestParam(name = "id", required = true) Integer id) {
        try {
            int i = userService.delUserById(id);
            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 获取所有用户
     */
    @RequestMapping(value = "/user/userList")
    @ResponseBody
    public Object userList() {
        List<User> userList = userService.queryUserList();
        return ResultMapUtil.getHashMapList(userList);
    }

}
